<template>
  <div id="spaceDetailPage">
    <!--    空间信息-->
    <a-flex justify="space-between">
      <h2>{{ space.spaceName }}</h2>
      <a-space>

        <a-button type="primary" :href="`/add_space?spaceId=${id}`" target="_blank"
          >创建空间</a-button>
        <a-button type="primary" @click="doCreatePicture" target="_blank"
        >创建图片</a-button>
        <a-tooltip :title="`占用空间:  ${formatSize(space.totalSize)} / ${formatSize(space.maxSize)}`">
          <a-progress  type="circle"
                       :size="42"
                       :percent="((space.totalSize * 100) / space.maxSize).toFixed(1)">

          </a-progress>
        </a-tooltip>

      </a-space>

    </a-flex>

    <div style="margin-bottom: 16px" />
    <PictureSearchFrom :on-search="onSearch" style="margin-bottom: 10px"></PictureSearchFrom>
    <!-- 图片列表 -->
    <PictureList :dataList="dataList" :loading="loading" :showOp="true" :onReload="fetchData" />
    <!-- 分页 -->
    <a-pagination
      style="text-align: right"
      v-model:current="seachrParams.current"
      v-model:pageSize="seachrParams.pageSize"
      :total="total"
      @change="onPageChange"
    />
  </div>
</template>

<script setup lang="ts">
import { computed, h, onMounted, reactive, ref } from 'vue'
import { listPictureVoByPageUsingPost } from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'

import { getSpaceVoByIdUsingGet } from '@/api/spaceController.ts'
import PictureList from '@/components/PictureList.vue'
import { formatSize } from '@/utils'
import { useRoute, useRouter } from 'vue-router'
import PictureSearchFrom from '@/components/PictureSearchFrom.vue'

interface Props {
  id: string | number
}

const props = defineProps<Props>()
const space = ref<API.SpaceVO>({})

// -------- 获取空间详情 --------
const fetchSpaceDetail = async () => {
  try {
    const res = await getSpaceVoByIdUsingGet({
      id: props.id,
    })
    if (res.data.code === 0 && res.data.data) {
      space.value = res.data.data
    } else {
      message.error('获取空间详情失败，' + res.data.message)
    }
  } catch (e: any) {
    message.error('获取空间详情失败：' + e.message)
  }
}
// 图片列表     ===========
const dataList = ref<API.PictureVO[]>([])
const total = ref(0)
const loading = ref(true)

// 搜索条件
const seachrParams = ref<API.PictureQueryRequest>({
  current: 1,
  pageSize: 12,
  sortField: 'createTime',
  sortOrder: 'descend',
})
// 搜索调用
const  onSearch = async  (newSeachrParams: API.PictureQueryRequest)=>{
     seachrParams.value = {
       ...seachrParams.value,
       ...newSeachrParams,
     }
     fetchData()
}
const router = useRouter()
// 创建图片并且携带 spaceId
const doCreatePicture = async () =>{
  router.push({
    path: '/add_picture',
    query: {
      spaceId: props.id,
    },
  })
}
// 分页参数  computed 计算函数  seachrParams 改变的时候可以跟着动态改变
const onPageChange = async (page: number, pageSize: number) => {
  seachrParams.value.current = page
  seachrParams.value.pageSize = pageSize
  fetchData()
}
const pagination = computed(() => {
  return {
    current: seachrParams.value.current,
    pageSize: seachrParams.value.pageSize,
    total: total.value,
    showSizeChanger: true,
  }
})
// 获取数据   ...seachrParams 数据展开
const fetchData = async () => {
  const params = {
    ...seachrParams.value,
    spaceId: props.id,
    nullSpaceId: false
  }
  const res = await listPictureVoByPageUsingPost(params)
  if (res.data.code == 0 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败' + res.data.message)
  }
  loading.value = false
}
const doSearch = () => {
  seachrParams.value.current = 1
  fetchData()
}

// 页面加载时候调用数据
onMounted(() => {
  fetchSpaceDetail()
  fetchData()
})
</script>

<style scoped>
#spaceDetailPage {
  margin-bottom: 16px;
}
</style>
